﻿//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Linq;
//using YmtAuth.Domain.Model.LoginAuth;
//using YmtAuth.Domain.Repository;
//using YmtSystem.CrossCutting;
//
//namespace YmtAuth.AppService
//{
//    [Obsolete("不要使用",true)]
//    public class ImportUser
//    {
//        private static IUserLoginBillsRepository UlBRepository { get { return LocalServiceLocator.GetService<IUserLoginBillsRepository>(); } }
//
//        public static void BatchImport(IEnumerable<Dictionary<string, object>> uData, string dbName = "Ymt_Auth_201406_1", string tbName = "ULC_20140627")
//        {
//            YmtSystemAssert.AssertArgumentNotNull(uData, "用户数据不能为空");
//            var ulcList = new List<UserLoginBill>();
//            foreach (var item in uData)
//            {
//                ulcList.Add(new UserLoginBill(item));
//            }
//            UlBRepository.AddRang(ulcList, dbName, tbName);
//        }
//
//        public static void Import(Dictionary<string, object> uData, string dbName = "Ymt_Auth_201406_1", string tbName = "ULC_20140627", bool checkExist = true)
//        {
//            YmtSystemAssert.AssertArgumentNotNull(uData, "用户数据不能为空");
//
//            var ulcList = new List<UserLoginBill>();
//            var data = new UserLoginBill(uData);
//            if (checkExist && !UlBRepository.Exists(UserLoginAuthSpecifications.MatchLoginId(data.LoginBills.LoginId)))
//            {
//                UlBRepository.Add(data, dbName, tbName);
//            }
//        }
//
//        public static void ImportPassword(Dictionary<string, object> uData, bool checkExist = true)
//        {
//            YmtSystemAssert.AssertArgumentNotNull(uData, "用户数据不能为空");
//
//            var ulcList = new List<UserLoginBill>();
//            var data = new UserLoginBill(uData);
//            var user = UlBRepository.FindOne(UserLoginAuthSpecifications.MatchUserId(data.UserId), true);
//            if (user != null)
//            {
//                if (user.LoginPassword != data.LoginPassword)
//                {
//                    UlBRepository.Update(UserLoginAuthSpecifications.MatchUserId(data.UserId), UserLoginAuthSpecifications.UpateUserLoginPassword(data.LoginPassword), "Ymt_Auth_201406", "ULC_201406");
//                }
//            }
//        }
//
//        public static void ImportThirdParty(Dictionary<string, object> uData, string dbName = "Ymt_Auth_201406_1", string tbName = "ULC_20140627", bool checkExist = true)
//        {
//            YmtSystemAssert.AssertArgumentNotNull(uData, "用户数据不能为空");
//
//            var ulcList = new List<UserLoginBill>();
//            var data = new UserLoginBill(uData);
//            var user = UlBRepository.FindOne(UserLoginAuthSpecifications.MatchUserId(data.UserId));
//            if (user != null)
//            {
//                if (data.LoginBills.Email != user.LoginBills.Email && !UlBRepository.Exists(UserLoginAuthSpecifications.NotMatchLoginMailByUserId(data.LoginBills.Email, data.UserId)))
//                {
//                    UlBRepository.Update(UserLoginAuthSpecifications.MatchUserId(user.UserId), UserLoginAuthSpecifications.UpateUserEmail(data.LoginBills.Email), dbName, tbName);
//
//                    var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\upuserEmail.log");
//                    System.IO.File.AppendAllText(logPath, string.Format("{0},\r\n", data.UserId, data.LoginBills.Email, data.TradingPassword));
//
//                }
//                if (string.IsNullOrEmpty(user.TradingPassword) && !string.IsNullOrEmpty(data.TradingPassword))
//                {
//                    UlBRepository.Update(UserLoginAuthSpecifications.MatchUserId(user.UserId), UserLoginAuthSpecifications.UpdateUserTradingPassword(data.TradingPassword), dbName, tbName);
//
//                    var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\upuserTradingPassword.log");
//                    System.IO.File.AppendAllText(logPath, string.Format("{0},\r\n", data.UserId, data.LoginBills.Email, data.TradingPassword));
//
//                }
//
//            }
//            else
//            {
//                if (!UlBRepository.Exists(UserLoginAuthSpecifications.MatchLoginId(data.LoginBills.LoginId)))
//                {
//                    UlBRepository.Add(data, dbName, tbName);
//
//                    var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\addthuser.log");
//                    System.IO.File.AppendAllText(logPath, string.Format("{0},\r\n", data.UserId));
//
//                }
//            }
//        }
//        private static readonly object o1 = new object();
//        private static readonly object o = new object();
//        private static int index;
//        public static void ImportUserMobile(int userId, string mobile, string dbName = "Ymt_Auth_201406_1", string tbName = "ULC_20140627", bool checkExist = true)
//        {
//            //if (checkExist && !UlBRepository.Exists(UserLoginAuthSpecifications.MatchLoginMobile(mobile)))
//            {
//                var user = UlBRepository.FindOne(UserLoginAuthSpecifications.MatchUserId(userId));
//                if (user != null)
//                {
//                    // if (string.IsNullOrEmpty(user.LoginBills.Mobile))
//                    {
//                        //user.LoginBills.Mobile = mobile;
//                        try
//                        {
//                            UlBRepository.Update(UserLoginAuthSpecifications.MatchUserId(userId), UserLoginAuthSpecifications.UpateUserMobile(mobile), dbName, tbName);
//                            System.Threading.Interlocked.Increment(ref index);
//                            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\upmobile3.log");
//                            System.IO.File.AppendAllText(logPath, string.Format("{0},{1}\r\n", userId, mobile));
//                            YmatouLoggingService.Info("已更新 {0},{1},index {2}", userId, mobile, index);
//                        }
//                        catch (Exception ex)
//                        {
//                            YmatouLoggingService.Error("用户 {0}更新异常 {1}", userId, ex);
//                        }
//                    }
//                }
//                //else
//                //{
//                //    lock (o)
//                //    {
//                //        var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\notexists.log");
//                //        System.IO.File.AppendAllText(logPath, string.Format("{0},\r\n", userId));
//                //        // YmatouLoggingService.Debug("用户,{0},不存在,手机,{1}", userId, mobile);
//                //    }
//                //}
//            }
//        }
//
//        public static void ReadMobile(IEnumerable<string> mobile)
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\mobile.log");
//            var mobileList = UlBRepository.Find(UserLoginAuthSpecifications.MathLoginMobileArray(mobile), MongoDB.Driver.Builders.Fields.Include("uId", "lBills.Mobile")).ToList();
//            mobileList.Each(e => System.IO.File.AppendAllText(logPath, string.Format("{0},{1}\r\n", e.UserId, e.LoginBills.Mobile)));
//        }
//
//
//        public static void ReadUsers(IEnumerable<int> userid)
//        {
//
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\users_8_8_01.log");
//            var mobileList = UlBRepository.Find(UserLoginAuthSpecifications.MathUserIdArray(userid), MongoDB.Driver.Builders.Fields.Include("uId", "lPas", "lBills.LoginId", "lBills.Email", "lBills.Mobile", "tPas", "Locked")).ToList();
//            mobileList.Each(e => System.IO.File.AppendAllText(logPath, string.Format("{0},{1},{2},{3},{4},{5},{6}\r\n", e.UserId, e.LoginBills.LoginId, e.LoginPassword, e.TradingPassword ?? "", e.LoginBills.Email ?? "", e.LoginBills.Mobile, "FALSE")));
//        }
//
//        public static void BulkUpdate(string db, string tb, int pageSizes, int pageCount)
//        {
//            var i = 32;
//            var watch = Stopwatch.StartNew();
//            for (; i < pageCount; i++)
//            {
//                var watch2 = Stopwatch.StartNew();
//                var result = UlBRepository.FindAll(db, tb, i, pageSizes);
//                UlBRepository.BulkUpdate(e =>
//                {
//                    var index = 0;
//                    foreach (var item in result)
//                    {
//                        if (item != null)
//                        {
//                            var newLoginId = item.LoginBills.LoginId.ToLower();
//                            if (newLoginId != item.LoginBills.LoginId)
//                            {
//                                //if (!UlBRepository.Exists(UserLoginAuthSpecifications.NotMatchLoginIdByUserId(newLoginId, item.UserId)))
//                                {
//                                    e.Find(UserLoginAuthSpecifications.MatchUserId(item.UserId))
//                                    .Update(UserLoginAuthSpecifications.UpateUserLoginIdOrEmail(item.LoginBills.Email, item.LoginBills.LoginId));
//
//                                    var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\upUserBills.log");
//                                    var str = string.Format("{0},{1},{2}\r\n", item.UserId, item.LoginBills.LoginId, item.LoginBills.Email);
//                                    System.IO.File.AppendAllText(logPath, str);
//                                }
//                            }
//                        }
//                        Console.WriteLine("{0}, {1}", index++, item.UserId);
//
//                    }
//                }, false, db, tb, r =>
//                {
//                    Console.WriteLine("upCount {0},index {1} , runTime {2}", r.ModifiedCount, i, watch2.Elapsed.TotalSeconds);
//                }, errHandle: err => YmatouLoggingService.Error("{0}", err.ToString()));
//            }
//            Console.WriteLine("更新记录 {0}，耗时 {1}", i * pageSizes, watch.Elapsed);
//            Console.Read();
//        }
//
//        public static void ReadAllUsers(string db, string tb, int pageSizes, int pageCount)
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser.csv");
//
//            var watch = Stopwatch.StartNew();
//            for (var i = 0; i < pageCount; i++)
//            {
//                var result = UlBRepository.FindAll(db, tb, i, pageSizes);
//                foreach (var item in result)
//                {
//                    if (item != null)
//                    {
//                        var str = string.Format("{0},{1},{2},{3}\r\n", item.UserId, item.LoginBills.LoginId, item.LoginBills.Email, item.LoginBills.Mobile);
//                        System.IO.File.AppendAllText(logPath, str);
//                    }
//                }
//                Console.WriteLine(i);
//            }
//            Console.WriteLine("end {0}", watch.Elapsed);
//            Console.Read();
//        }
//
//        public static void ReadUsersLoginHistory(string db, string tb, int pageSizes, int pageCount)
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\loginhistory-2014-8-8-mobile.csv");
//            var logPath2 = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\loginhistory-2014-8-8-mail.csv");
//            var logPath3 = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\loginhistory-2014-8-8-loginId.csv");
//
//            var logHistory = LocalServiceLocator.GetService<IYmtUserLoginHistoryRepository>();
//            var loginList = logHistory.Find(e => e.iAction > 0 && (e.dAddTime >= Convert.ToDateTime("2014-08-08 20:00:00") && e.dAddTime <= Convert.ToDateTime("2014-08-11 23:59:59")), "Ymt_LoginHistory_201402", "LoginHistory_201408").ToList().OrderBy(e => e.iAction).Distinct(new A());
//            foreach (var item in loginList)
//            {
//                var str = string.Format("{0},{1},{2}\r\n", item.iAction, item.sLoginId, item.sClientType);
//                if (item.sLoginId.Contains("@"))
//                    System.IO.File.AppendAllText(logPath2, str);
//                else if (item.sLoginId.Length == 11 && item.sLoginId.IsMobile())
//                    System.IO.File.AppendAllText(logPath, str);
//                else
//                    System.IO.File.AppendAllText(logPath3, str);
//            }
//            Console.WriteLine("end");
//            Console.Read();
//        }
//
//        public static void ImportUserRegister()
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\loginhistory-2014-8-8-UserReg-1.csv");
//            var regRepo = LocalServiceLocator.GetService<IUserRegisterEventRepository>();
//            var query = regRepo.Find(e => string.IsNullOrEmpty(e.LoginMail) && (e.CreateTime > DateTime.Parse("2014-07-31 23:59:59") && e.CreateTime <= DateTime.Parse("2014-08-01 23:59:59")), "Ymt_LoginHistory_201402", "UserReg_201407").ToList();
//            Console.WriteLine(query.Count);
//            Console.Read();
//            query.Each(e =>
//            {
//                var str = string.Format("{0},{1},{2},{3},{4},{5}\r\n", e.UserId, e.LoginId, e.LoginMail, e.Mobile, e.RegSource, e.TerminalSource);
//                System.IO.File.AppendAllText(logPath, str);
//            });
//            Console.WriteLine("写入文件完成");
//            var _mobile = query.Where(e => !string.IsNullOrEmpty(e.Mobile));
//            var _mobile_day = _mobile.GroupBy(e => e.CreateTime.ToShortDateString());
//            Console.WriteLine("每天手机注册{0}", _mobile_day.Count());
//            var regMobilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\2014-8-mobile-UserReg.csv");
//            _mobile_day.Each(e =>
//            {
//                var str1 = string.Format("{0}-{1}\r\n", e.Key, e.Count());
//                System.IO.File.AppendAllText(regMobilePath, str1);
//            });
//            var MailPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\2014-8-Mail-UserReg.csv");
//            var _email = query.Where(e => !string.IsNullOrEmpty(e.LoginMail));
//            var _email_day = _email.GroupBy(e => e.CreateTime.ToShortDateString());
//            Console.WriteLine("每天邮箱注册{0}", _email_day.Count());
//            _email_day.Each(e =>
//            {
//                var str1 = string.Format("{0}-{1}\r\n", e.Key, e.Count());
//                System.IO.File.AppendAllText(MailPath, str1);
//            });
//            var WeiboPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\2014-8-WeiBo-UserReg.csv");
//            var _weibo = query.Where(e => e.RegSource == "Weibo");
//            var _weibo_day = _weibo.GroupBy(e => e.CreateTime.ToShortDateString());
//            Console.WriteLine("每天微博注册{0}\r\n", _weibo_day.Count());
//            _weibo_day.Each(e =>
//            {
//                var str1 = string.Format("{0}-{1}\r\n", e.Key, e.Count());
//                System.IO.File.AppendAllText(WeiboPath, str1);
//            });
//            var AliPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\2014-8-Ali-UserReg.csv");
//            var _ali = query.Where(e => e.RegSource == "Alipay");
//            var _ali_day = _ali.GroupBy(e => e.CreateTime.ToShortDateString());
//            Console.WriteLine("每天阿里注册{0}", _ali_day.Count());
//            _ali_day.Each(e =>
//            {
//                var str1 = string.Format("{0}-{1}\r\n", e.Key, e.Count());
//                System.IO.File.AppendAllText(AliPath, str1);
//            });
//            Console.WriteLine("end");
//            Console.Read();
//        }
//        class A : IEqualityComparer<YmtAuth.Domain.EventHistory.YmtUserLoginEvent>
//        {
//
//            public bool Equals(Domain.EventHistory.YmtUserLoginEvent x, Domain.EventHistory.YmtUserLoginEvent y)
//            {
//                return x.iAction == x.iAction;
//            }
//
//            public int GetHashCode(Domain.EventHistory.YmtUserLoginEvent obj)
//            {
//                return obj.iAction.GetHashCode();
//            }
//        }
//        public static void ImportUserRegisterHistory(string dbName, string collectionName)
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\registerhistory-2014-9.csv");
//            var regRepo = LocalServiceLocator.GetService<IUserRegisterEventRepository>();
//            var query = regRepo.Find(e => e.CreateTime >= Convert.ToDateTime("2014-09-01 00:00:00"),dbName,collectionName).OrderBy(e => e.CreateTime);
//            query.Each(e =>
//            {
//                var str = string.Format("{0},{1},{2},{3},{4},{5},{6},{7}\r\n", e.UserId, e.LoginId, e.LoginMail, e.Mobile, e.RegSource, e.ChannelSource, e.TerminalSource, e.CreateTime.ToLocalTime());
//                System.IO.File.AppendAllText(logPath, str);
//            });
//        }
//        public static void ImportUserloginHistory(string dbName, string collectionName)
//        {
//            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\loginhistory-2014-9.csv");
//            var regRepo = LocalServiceLocator.GetService<IYmtUserLoginHistoryRepository>();
//            var query = regRepo.Find(e => e.dAddTime >= Convert.ToDateTime("2014-09-01 00:00:00"),dbName,collectionName);
//            query.Each(e =>
//            {
//                var str = string.Format("{0},{1},{2},{3},{4},{5}\r\n", e.sLoginId, e.iAction, e.dAddTime.ToLocalTime(), e.sClientType, e.sLoginIp, e.sSummary);
//                System.IO.File.AppendAllText(logPath, str);
//            });
//        }
//    }
//}
